import {Goods} from "../model/Goods.js";
import {where} from "sequelize";

let fn_list = async (ctx) => {
    let id = ctx.params.id || 0
    let obj = ctx.request.body || ""
    let goods = await Goods.findAll({where: {}})
    ctx.body = {
        data: goods,
        code: 1000,
        msg: "显示成功"
    }
}
let fn_byId = async (ctx) => {
    let id = ctx.params.id || 0
    let obj = ctx.request.body || ""
    let goods = await Goods.findByPk(id)
    ctx.body = {
        data: goods,
        code: 1000,
        msg: "查询成功"
    }
}
let fn_add = async (ctx) => {
    let id = ctx.params.id || 0
    let obj = ctx.request.body || ""
    if (obj) {
        await Goods.create(obj)
    }
    ctx.body = {
        data: obj,
        code: 1000,
        msg: "新增成功"
    }
}

let fn_upd = async (ctx) => {
    let id = ctx.params.id || 0
    let obj = ctx.request.body || ""
    let goods = await Goods.update(obj, {where: {id: id}})

    ctx.body = {
        data: obj,
        code: 1000,
        msg: "修改成功"
    }
}
let fn_del = async (ctx) => {
    let id = ctx.params.id || 0
    let obj = ctx.request.body || ""
    let goods = await Goods.destroy({where: {id: id}})
    if (goods) {
        ctx.body = {
            data: obj,
            code: 1000,
            msg: "删除成功"
        }
    } else {
        ctx.body = {
            data: obj,
            code: 1000,
            msg: "删除失败"
        }
    }
}
export default [
    {
        method: "get",
        url: "/goods",
        fn: fn_list
    },
    {
        method: "get",
        url: "/goods/:id",
        fn: fn_byId
    }, {
        method: "post",
        url: "/goods",
        fn: fn_add
    }, {
        method: "put",
        url: "/goods/:id",
        fn: fn_upd
    }, {
        method: "del",
        url: "/goods/:id",
        fn: fn_del
    },

]